package co.yixiang.common.security.service;

import co.yixiang.common.security.domain.JwtUser;
import co.yixiang.mall.common.feign.RemoteMemberUserService;
import co.yixiang.retail.common.domain.UserInfo;
import co.yixiang.retail.common.dto.DeptSmallDto;
import co.yixiang.retail.common.dto.JobSmallDto;
import co.yixiang.retail.common.dto.UserDto;
import co.yixiang.retail.common.feign.RemoteStoreUserService;
//import co.yixiang.upms.common.system.dto.DeptSmallDto;
//import co.yixiang.upms.common.system.dto.JobSmallDto;
//import co.yixiang.upms.common.system.dto.UserDto;
//import co.yixiang.upms.common.system.feign.RemoteUserService;
//import co.yixiang.upms.common.system.vo.UserInfo;
import co.yixiang.upms.common.system.feign.RemoteUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.*;
import java.util.stream.Collectors;


/**
 * Created by hupeng on 2020/9/25.
 */
public abstract class BaseUserDetailService implements UserDetailsService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected RemoteUserService remoteUserService;
    @Autowired
    protected RemoteMemberUserService remoteMemberUserService;
    @Autowired
    protected RemoteStoreUserService remoteStoreUserService;
    @Autowired
    protected JwtPermissionService permissionService;

    @Override
    public UserDetails loadUserByUsername(String var1) throws UsernameNotFoundException {
        UserInfo userInfo = getUser(var1);

        UserDto user = userInfo.getUserDto();

        Set<String> permissions = userInfo.getPermissions();

        Collection<SimpleGrantedAuthority> simpleGrantedAuthorities = null;

        if(permissions != null && !permissions.isEmpty()){
            simpleGrantedAuthorities = permissions.stream().map(SimpleGrantedAuthority::new)
                            .collect(Collectors.toList());
        }else {
            simpleGrantedAuthorities =  permissionService.mapToGrantedAuthorities(user);
        }

        return createJwtUser(user,simpleGrantedAuthorities);
    }

    protected abstract UserInfo getUser(String var1) ;

    private UserDetails createJwtUser(UserDto user, Collection<SimpleGrantedAuthority> simpleGrantedAuthorities) {
        return new JwtUser(
                user.getId(),
                user.getUsername(),
                user.getNickName(),
                user.getSex(),
                user.getPassword(),
                user.getAvatar(),
                user.getEmail(),
                user.getPhone(),
                Optional.ofNullable(user.getDept()).map(DeptSmallDto::getName).orElse(null),
                Optional.ofNullable(user.getJob()).map(JobSmallDto::getName).orElse(null),
                user.getRetail(),
                user.getRetailType(),
                user.getPermit(),
                simpleGrantedAuthorities,
                user.getEnabled(),
                user.getCreateTime(),
                user.getLastPasswordResetTime()

        );
    }



}
